var http = require('http');
var https = require('https');
var fs = require('fs');
var cheerio = require('cheerio');
var request = require('request');

var i = 0;
var domain = 'http://www.dytt8.net';

var spider1 = ( x ) => {
    startRequests(x);
}

var startRequests = ( x ) => {
    
    http.get( x, (res)=>{
        let html = '';
        let titles = [];
        res.setEncoding('utf-8');
        res.on('data', (chunk)=>{
            html += chunk;
        }),

        res.on('end', ()=>{
            let $ = cheerio.load(html);;
            let time = $('.article-info a:first-child').next().text().trim();

            let imgs = $('img');

            imgs.each((index, item)=>{
                let tmpindex = item.attribs['src'].lastIndexOf('/');
                let img_name = item.attribs['src'].substr(tmpindex);
                let img_src = '';
                if('http' == item.attribs['src'].substr(0,4)){
                    img_src = item.attribs['src'];
                } else {
                    img_src = domain + item.attribs['src'];
                }


                request(img_src)
                    .pipe(fs.createWriteStream('./image/'+ img_name));


            });
        });
    } );

}

module.exports = spider1;